package com.rongc.client.core.utils;

import android.content.Context;
import android.inputmethodservice.Keyboard;
import android.inputmethodservice.KeyboardView;
import android.view.View;

import com.rongc.client.freight.R;

import java.util.List;

/**
 * Created by Bruce on 2017/7/28.
 */
public class KeyboardUtil {
    private Context ctx;
    private KeyboardView keyboardView;
    private Keyboard k2;// 数字键盘
    List<String> datas;

    public KeyboardUtil(View act, Context ctx, List<String> datas) {
        this.ctx = ctx;
        this.datas = datas;
//        k1 = new Keyboard(ctx, R.xml.symbols);
        k2 = new Keyboard(ctx, R.xml.symbols);
        keyboardView = (KeyboardView) act.findViewById(R.id.keyboard_view);
        keyboardView.setKeyboard(k2);
        keyboardView.setEnabled(false);
        keyboardView.setPreviewEnabled(false);
        keyboardView.setOnKeyboardActionListener(listener);
    }

    public interface KeyBoardListener{
        void onChange();
        void onComplete();
    }
    public KeyBoardListener mListener;
    public void setListener(KeyBoardListener mListener){
        this.mListener = mListener;
    }


    private KeyboardView.OnKeyboardActionListener listener = new KeyboardView.OnKeyboardActionListener() {
        @Override
        public void swipeUp() {
        }

        @Override
        public void swipeRight() {
        }

        @Override
        public void swipeLeft() {
        }

        @Override
        public void swipeDown() {
        }

        @Override
        public void onText(CharSequence text) {
        }

        @Override
        public void onRelease(int primaryCode) {
        }

        @Override
        public void onPress(int primaryCode) {
        }

        @Override
        public void onKey(int primaryCode, int[] keyCodes) {


            if (primaryCode == Keyboard.KEYCODE_CANCEL) {// 完成
                hideKeyboard();
            } else if (primaryCode == Keyboard.KEYCODE_DELETE) {// 回退
                if(datas.size() > 0)
                    datas.remove(datas.size()-1);
            } else {
                if(datas.size() < 6)
                    datas.add(Character.toString((char) primaryCode));
            }
            if(mListener != null) {
                mListener.onChange();
                if (datas.size() == 6){
                    mListener.onComplete();
                }
            }
        }
    };
    public void showKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.GONE || visibility == View.INVISIBLE) {
            keyboardView.setVisibility(View.VISIBLE);
        }
    }

    public void hideKeyboard() {
        int visibility = keyboardView.getVisibility();
        if (visibility == View.VISIBLE) {
            keyboardView.setVisibility(View.INVISIBLE);
        }
    }

    private boolean isword(String str){
        String wordstr = "abcdefghijklmnopqrstuvwxyz";
        if (wordstr.indexOf(str.toLowerCase())>-1) {
            return true;
        }
        return false;
    }
}
